package com.jlastu.znb.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;
import com.jlastu.znb.buy.buyManager;
import com.jlastu.znb.contract.MineContract;
import com.jlastu.znb.database.DBHelper;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.entity.Photo;
import com.jlastu.znb.gwc.CartManager;
import com.jlastu.znb.sc.scManager;
import com.jlastu.znb.utils.SpUtils;

import java.util.List;

public class GoodsDetailActivity extends AppCompatActivity {

    private DBHelper dbHelper;
    private int goodsId;
    private CartManager cartManager;
    private scManager scManager;
    private buyManager buyManager;
    private SpUtils mSpUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_detail);
        mSpUtils = new SpUtils(this);
        cartManager = new CartManager(this);
       scManager = new scManager(this);
        buyManager = new buyManager(this);
        dbHelper = new DBHelper(this);

        goodsId = getIntent().getIntExtra("GOODS_ID", -1);

        //添加购物车
        Button addToCartButton = findViewById(R.id.add_to_cart_button);
        addToCartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int userId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
                if (goodsId != -1) {

                    cartManager.addToCart(goodsId, 1, userId);
                    Toast.makeText(GoodsDetailActivity.this, "添加购物车成功", Toast.LENGTH_SHORT).show();
                    // 添加商品到购物车
                }
            }
        });
//收藏
        LinearLayout addToCartButton1 = findViewById(R.id.scc);
        addToCartButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int userId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
                if (goodsId != -1) {

                    scManager.addTosc(goodsId, 1, userId);
                    Toast.makeText(GoodsDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    // 收藏商品
                }
            }
        });
        Button buyButton = findViewById(R.id.buy11);
        buyButton.setOnClickListener(v -> {
            int userId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
            buyManager.addTobuy(goodsId, 1, userId);
            Intent intentToFK = new Intent(GoodsDetailActivity.this, fkActivity.class);
            intentToFK.putExtra("GOODS_ID", goodsId); // 传递商品ID
            startActivity(intentToFK);
        });

//跳转客服
        LinearLayout myLinearLayout = findViewById(R.id.kefutiaozhuan);
        myLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsDetailActivity.this, com.jlastu.znb.message.KMainActivity.class);
                startActivity(intent);
            }
        });
        //设置商品详情信息
        if (goodsId != -1) {
            Goods goods = dbHelper.getGoodsById(goodsId);
            if (goods != null) {
                TextView nameView = findViewById(R.id.item_name);
                TextView priceView = findViewById(R.id.item_price);
                TextView messageView= findViewById(R.id.message);
                ImageView imageView = findViewById(R.id.item_image);
                List<Photo> photos = goods.getPhotoList();
                if (photos != null && !photos.isEmpty()) {
                    Photo firstPhoto = photos.get(0);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(firstPhoto.getData(), 0, firstPhoto.getData().length);
                    imageView.setImageBitmap(bitmap);
                }
                messageView.setText(goods.getMessage());
                nameView.setText(goods.getName());
                priceView.setText(String.format("¥ %s/%s", goods.getPrice(), goods.getUnit()));
            }
        }

    }
    private int getUserId() {
        return mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
    }

}